package com.gqz.aircraftwar.mvp.model.aircraft

import android.graphics.Canvas
import com.gqz.aircraftwar.App
import com.gqz.aircraftwar.R
import com.gqz.aircraftwar.mvp.model.bullet.BulletManager
import com.gqz.aircraftwar.mvp.view.BasePath
import com.gqz.aircraftwar.util.MyEnum
import com.gqz.aircraftwar.util.d
import com.gqz.aircraftwar.util.i
import kotlinx.coroutines.*
import java.lang.Exception
import java.util.concurrent.CopyOnWriteArrayList
import kotlin.system.measureTimeMillis

/**
 * Descraption
 *
 * @author gqz
 * @data 2020/4/3
 */
class EnemyManager {
    private val TAG: String = "EnemyManager-->"

    private object Holder {
        val instance = EnemyManager()
        const val delay = 1000L / 30L
    }

    companion object {
        var instance = Holder.instance
    }

    var isAllDead = true
    var enemyList = CopyOnWriteArrayList<EnemyAircraft>()
    val ctx = newSingleThreadContext("Enemy")
    var job: Job? = null

    fun start() {
        enemyList.clear()
        try {
            job = GlobalScope.launch(ctx) {
                i(TAG, "start--> enemyManager worked in ${Thread.currentThread().name}")
                while (App.isRunning) {
//                    d(TAG, "running-->")
                    var it = enemyList.filter { !it.free }
                    isAllDead = it.isEmpty()
                    for (i in it.indices) {
                        it[i].moveByPath(i * it[i].appearance.height + 30f)
                        it[i].hit(PlayerAircraft.getInstant())//敌机是否撞击玩家
                        var ep = it[i]
                        //玩家子弹和敌机的碰撞
                        BulletManager.instance.bulletList.filter {
                            !it.free && it.dir == MyEnum.Dir.up
                        }.forEach {
                            it.hit(ep)
                        }
                        isAllDead = false
                    }
                    delay(Holder.delay)

                }
                enemyList.clear()
                isAllDead = true
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun stop() {
        enemyList.clear()
        job?.cancel()
    }

    fun generateGroup(count: Int, path: BasePath) {
        for (i in 1..count) {
            if (i >= count - 1) {
                var r = (1..4).random()
                if (r <= 3) {
                    generate(MyEnum.EnemyType.advanced, path)
                } else {
                    generate(MyEnum.EnemyType.elite, path)
                }
            } else {
                generate(MyEnum.EnemyType.common, path)
            }
        }
        isAllDead = false
    }

    fun generate(type: MyEnum.EnemyType, path: BasePath) {
        var enemy = enemyList.find { it.free }
        if (enemy == null) {
            enemy = EnemyAircraft()
            enemyList.add(enemy)
        }
        enemy.free = false
        enemy.path = path
        when (type) {
            MyEnum.EnemyType.common -> {
                enemy.set(type, R.drawable.plane, 5f, 3, 3f, 0.3f)
            }
            MyEnum.EnemyType.advanced -> {
                enemy.set(type, R.drawable.plane, 8f, 4, 4f, 0.5f)
            }
            MyEnum.EnemyType.elite -> {
                enemy.set(type, R.drawable.plane, 10f, 5, 5f, 0.7f)
            }
            MyEnum.EnemyType.boss -> {
                enemy.set(type, R.drawable.plane, 20f, 8, 3f, 0.9f)
            }
        }
        enemy.start()
    }

    fun drawAll(canvas: Canvas) {
        enemyList.forEach { it.draw(canvas) }
    }

}